#include <dbg.h>
#include <gtest/gtest.h>
using namespace std;

#ifdef TWO_VECTOR
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        if (root == nullptr) {
            return {};
        }

        vector<vector<int>> res;
        vector<TreeNode*> cur{root};
        while (!cur.empty()) {
            vector<TreeNode*> next;
            vector<int> vals;
            for (auto& node : cur) {
                vals.emplace_back(node->val);
                if (node->left != nullptr) {
                    next.emplace_back(node->left);
                }
                if (node->right != nullptr) {
                    next.emplace_back(node->right);
                }
            }
            cur = next;
            res.emplace_back(vals);
        }
        return res;
    }
};
#endif

#ifndef QUEUE
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        if (root == nullptr) {
            return {};
        }

        vector<vector<int>> res;
        queue<TreeNode*> que;
        que.push(root);
        while (!que.empty()) {
            vector<int> vals;
            for (int i = que.size(); i > 0; --i) {
                TreeNode* node = que.front();
                que.pop();
                vals.emplace_back(node->val);
                if (node->left != nullptr) {
                    que.push(node->left);
                }
                if (node->right != nullptr) {
                    que.push(node->right);
                }
            }
            res.emplace_back(std::move(vals));  // 移动语义，避免拷贝
        }
        return res;
    }
};
#endif

TEST(TEST1, levelOrder) {
    Solution s;

    TreeNode* root = new TreeNode(3);
    root->left = new TreeNode(9);
    root->right = new TreeNode(20);
    root->right->left = new TreeNode(15);
    root->right->right = new TreeNode(7);

    vector<vector<int>> res{{3}, {9, 20}, {15, 7}};
    EXPECT_EQ(s.levelOrder(root), res);
}

TEST(TEST2, levelOrder) {
    Solution s;

    TreeNode* root = new TreeNode(1);

    vector<vector<int>> res{{1}};
    EXPECT_EQ(s.levelOrder(root), res);
}

TEST(TEST3, levelOrder) {
    Solution s;

    TreeNode* root = nullptr;

    vector<vector<int>> res{};
    EXPECT_EQ(s.levelOrder(root), res);
}

int main(int argc, char* argv[]) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}